/*
 * MessageOut.java
 *
 * Created on May 22, 2007, 12:11 AM
 *
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package base;

import tal.drivechain.variable.Variable;
import tal.drivechain.plugin.Module;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.SetProperty;

/**
 *
 * @author Main
 */
public class Schedule extends Module {
    
    private Variable var = null;
    private long delay = 0;
    private SendThread send = null;
    private Calendar schedule = null;

    @Override
    public void setupProperties() {
        setSchedule(props.getProperty("schedule"), null);
        setOutput(props.getProperty("output"), null);
    }
    
    public void setSchedule(Object value, SetProperty.Flag flag) {
        if (value==null) value = Calendar.getInstance();
        schedule = (Calendar)value;
        delay = schedule.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
        if (infoFlag)
            log("will fire in " + this.delay + "ms (on " + new SimpleDateFormat("d.M.y,H:m:s").format(((Calendar)value).getTime()) + ")", MSG.INFO);
        if (delay<0) {
            log("The scheduled time has already passed.", MSG.WARNING);            
            delay = 0;
        }
        schedule();
    } public Object getSchedule() { return schedule; }

    public void setOutput(Object value, SetProperty.Flag flag) {
        var = (Variable)value;
    } public Object getOutput() { return var; }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (!in.getName().equals("init") || channel!=Channel.GLOBALMESSAGE) return;
        schedule();
    }
    
    public void fireMessage() {
        try {
            if (infoFlag) log("Sending scheduled message: " + var, MSG.INFO);
        } catch (IllegalArgumentException ie) {
            log(ie.getMessage(), MSG.WARNING);
        }
        sendToOutputs(var, -1);
        stop();
    }

    private void schedule() {
        if (delay!=0) {
            try {
                if (send!=null && send.isAlive())
                    send.join();
                send = new SendThread((long)delay, this);
                send.start();
            } catch (Exception ie) {
                log(ie.getMessage(), MSG.WARNING);
            } 
        } else fireMessage();
    }
    
    class SendThread extends Thread {
        private long delay;
        private Schedule m;
        
        public SendThread(long delay, Schedule m) {
            this.delay = delay;
            this.m = m;
        }
        
        @Override
        public void run() {
            synchronized(this) {
                try {
                    this.wait(delay);
                } catch (InterruptedException ie) { return; }
            }
            m.fireMessage();
        }
    }
}


